let main = document.querySelector("#main");
fetch("http://chst.vip:1234/api/getcategorytitle")
    .then(body => body.json())
    .then(res => {
        // console.log(res);
        let html = "";
        res.result.forEach(item => {
            html += `
                <div id=${item.titleId}>
                    ${item.title}
                </div><ul></ul>
                `
        })
        main.innerHTML = html;

        // console.log(res);
        let divs = document.querySelectorAll('#main>div');
        let ul = document.querySelectorAll("ul");
        console.log(ul);
        Array.from(divs).forEach(div => {
            // console.log(div);
            div.isClick = false;
            let i = 0
            div.onclick = function() {
                // console.log(this);
                let id = this.id;
                let name = this.getAttribute("id")
                    // console.log(name);
                if (id === name) {
                    let shopDiv = this;
                    this.isClick = !this.isClick;
                    fetch("http://chst.vip:1234/api/getcategory?titleid=" + id)
                        .then(body => body.json())
                        .then(res => {
                            console.log(res.result.length);
                            let html = "";
                            res.result.forEach(item => {
                                html += `
                            <li categoryId=${item.categoryId}>${item.category}</li>                
                            `
                            })

                            ul[id].innerHTML = html;
                            ul[id].style.height = Math.ceil(res.result.length / 4 * 50) + 'px'
                                // console.log(res.result[i].category);
                            let lis = document.querySelectorAll("#main ul li");
                            // console.log(lis);
                            [...lis].forEach(li => {
                                li.onclick = function() {
                                    console.log(li);
                                    let categoryid = this.getAttribute("categoryId")
                                    let category = this.innerText
                                    location.href = "../pages/bijia.html?title=" + category + "&" + "categoryid=" + categoryid
                                }
                            })
                            if (this.isClick) {
                                ul[id].style.display = 'block';

                            } else {
                                ul[id].style.display = 'none'

                            }
                        })

                }
            }




        })
    })